import { OssService } from '@/core/oss.service';
import { halt, ok } from '@/utils';
import {
  DiskStorage,
  DiskStorageFile,
  FileInterceptor,
  UploadedFile,
} from '@blazity/nest-file-fastify';
import { Controller, Post, UseInterceptors } from '@nestjs/common';
import { BaseController } from './base.controller';

@Controller('system')
export class SystemController extends BaseController {
  constructor(private readonly ossService: OssService) {
    super();
  }

  @Post('upload')
  @UseInterceptors(
    FileInterceptor('file', {
      storage: new DiskStorage({
        removeAfter: true,
      }),
    }),
  )
  async upload(@UploadedFile() file: DiskStorageFile) {
    const res = await this.ossService.put(
      Date.now() + file.filename,
      file.path,
    );
    if (res.res.status !== 200) {
      await this.log.info(res);
      return halt('上传失败');
    }
    return {
      ...ok(),
      errno: 0,
      data: {
        name: res.name,
        url: res.url,
      },
    };
  }
}
